package com.sykj.why.controller.front.menu;

import com.sykj.why.exception.WhyException;
import com.sykj.why.handler.Response;
import com.sykj.why.mongo.document.entity.MenuCategoryT;
import com.sykj.why.mongo.document.entity.MenuT;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.MenuCategoryTService;
import com.sykj.why.mongo.service.MenuTService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Eleven
 * @date 2019/6/18
 * @desc 前端菜单控制访问层
 */

@RestController
@RequestMapping(value = "/front/menu")
@Slf4j
@Api(value = "/front/menu",description = "前端菜单控制访问层")
public class FrontMenuController {

    @Autowired
    private MenuTService menuTService;

    @Autowired
    private MenuCategoryTService menuCategoryTService;

    /**
     * 通过栏目Id查询菜单列表
     * @param programaId
     * @return
     */
    @RequestMapping(value = "/findList/{programaId}",method = RequestMethod.GET)
    @ApiOperation(value = "/findList/{programaId}",notes = "通过栏目Id查询菜单列表")
    public ResponseEntity findList(@PathVariable String programaId){
        try {
            return Response.success("查询成功",menuTService.findMenuTreeByProgramaIdAndPid(programaId,"0"));
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("查询菜单列表出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 通过分类ID查询菜单
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "/findByCategoryId/{categoryId}",method = RequestMethod.GET)
    @ApiOperation(value = "/findByCategoryId/{categoryId}",notes = "通过分类ID查询菜单")
    public ResponseEntity findByCategoryId(@PathVariable String categoryId){
        try {
            MenuT menuT = null;
            MenuCategoryT menuCategoryT = menuCategoryTService.findById(categoryId);
            if (menuCategoryT!=null){
                if (menuCategoryT.getMenuId()!=null){
                    menuT = menuTService.findById(menuCategoryT.getMenuId());
                }
            }
            return Response.success("查询成功",menuT);
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("通过分类ID查询菜单出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

}
